package com.deloitte.payrollmgtsys.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	/**
	 * In this case intercept the request BEFORE it reaches the controller
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String userEmail = (String) request.getSession().getAttribute(
				"userEmail");

		if ((userEmail == null || userEmail.equals("")) && 
				!request.getRequestURI().startsWith("/mvc/signIn") &&
						!request.getRequestURI().startsWith("/mvc/getToken") && 
						!request.getRequestURI().startsWith("/mvc/getUserInfo")) {
			request.getSession().setAttribute("orig_url", request.getRequestURI());
			response.sendRedirect("/mvc/signIn");
		}

		return true;
	}

}
